<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
     <el-form-item label="订单编号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="课程名称" prop="courseName">
        <el-input
          v-model="queryParams.courseName"
          placeholder="请输入课程名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
       <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户成员" prop="userMemberName">
        <el-input
          v-model="queryParams.userMemberName"
          placeholder="请输入用户成员id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.course_infos_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="报名时间">
          <el-date-picker size="small" v-model="uploadTime" @change="handleQuery" unlink-panels :picker-options="pickerOptions" 　 style="width: 220px" value-format="yyyy-MM-dd" type="daterange"
                          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']"></el-date-picker>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
    <!--
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['course:userCourseInfos:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['course:userCourseInfos:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['course:userCourseInfos:remove']"
        >删除</el-button>
      </el-col>
      -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['course:userCourseInfos:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userCourseInfosList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
       <el-table-column label="订单编号" align="center" prop="orderNo" min-width="150" />
      <el-table-column label="课程名称" align="center" prop="courseName" min-width="220"/>
      <el-table-column label="用户名称" align="center" prop="userName" min-width="150"/>
      <el-table-column label="用户成员" align="center" prop="userMemberName" min-width="150"/>
      <el-table-column label="节数" align="center" prop="num" />
      <el-table-column label="日期" align="center" prop="date" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.date, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="价格" align="center" prop="price" />
      <el-table-column label="金额" align="center" prop="money" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.course_infos_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <!--
      <el-table-column label="操作人id" align="center" prop="adminId" />
        -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            v-if="scope.row.status==1"
            type="text"
            icon="el-icon-edit"
            @click="handleCancelBook(scope.row)"
            v-hasPermi="['course:userCourseInfos:edit']"
          >取消预约</el-button>
          <el-button
            size="mini"
            v-if="scope.row.status==1"
            type="text"
            icon="el-icon-add"
            @click="handleCheckBook(scope.row)"
            v-hasPermi="['course:userCourseInfos:remove']"
          >核销</el-button>

           <el-button
              size="mini"
              v-if="scope.row.status==2"
              type="text"
              icon="el-icon-edit"
              @click="handleCancelCheckBook(scope.row)"
              v-hasPermi="['course:userCourseInfos:remove']"
            >取消核销</el-button>
        </template>
      </el-table-column>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学员报名使用详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="对应user_courses的id" prop="userCourseId">
          <el-input v-model="form.userCourseId" placeholder="请输入对应user_courses的id" />
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="用户成员id" prop="userMemberId">
          <el-input v-model="form.userMemberId" placeholder="请输入用户成员id" />
        </el-form-item>
        <el-form-item label="课程id" prop="courseId">
          <el-input v-model="form.courseId" placeholder="请输入课程id" />
        </el-form-item>
        <el-form-item label="节数" prop="num">
          <el-input v-model="form.num" placeholder="请输入节数" />
        </el-form-item>
        <el-form-item label="日期" prop="date">
          <el-date-picker clearable
            v-model="form.date"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="金额" prop="money">
          <el-input v-model="form.money" placeholder="请输入金额" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.course_infos_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="操作人id" prop="adminId">
          <el-input v-model="form.adminId" placeholder="请输入操作人id" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUserCourseInfos, getUserCourseInfos, delUserCourseInfos, addUserCourseInfos, updateUserCourseInfos } from "@/api/course/userCourseInfos";

export default {
  name: "UserCourseInfos",
  dicts: ['course_infos_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学员报名使用详情表格数据
      userCourseInfosList: [],
      uploadTime: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userCourseId: null,
        userId: null,
        userMemberId: null,
        courseId: null,
        num: null,
        date: null,
        price: null,
        money: null,
        status: null,
        adminId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userCourseId: [
          { required: true, message: "对应user_courses的id不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "用户id不能为空", trigger: "blur" }
        ],
        userMemberId: [
          { required: true, message: "用户成员id不能为空", trigger: "blur" }
        ],
        courseId: [
          { required: true, message: "课程id不能为空", trigger: "blur" }
        ],
        date: [
          { required: true, message: "日期不能为空", trigger: "blur" }
        ],
        price: [
          { required: true, message: "价格不能为空", trigger: "blur" }
        ],
        money: [
          { required: true, message: "金额不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        remark: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ],
        adminId: [
          { required: true, message: "操作人id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
   if (null != this.$route.query.orderNo) {
        this.queryParams.orderNo = this.$route.query.orderNo;
    }else{
        this.uploadTime[0] = this.getNearDay(-30);
        this.uploadTime[1] = this.getNearDay(0);
    }
    this.getList();
  },
  methods: {
    /** 查询学员报名使用详情列表 */
    getList() {
      this.loading = true;
       if (this.uploadTime) {
            this.queryParams.beginDate = this.uploadTime[0];
            this.queryParams.endDate = this.uploadTime[1];
        } else {
            this.queryParams.beginDate = '';
            this.queryParams.endDate = '';
      }
      listUserCourseInfos(this.queryParams).then(response => {
        this.userCourseInfosList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userCourseId: null,
        userId: null,
        userMemberId: null,
        courseId: null,
        num: null,
        date: null,
        price: null,
        money: null,
        status: null,
        remark: null,
        adminId: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学员报名使用详情";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getUserCourseInfos(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学员报名使用详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateUserCourseInfos(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUserCourseInfos(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除学员报名使用详情编号为"' + ids + '"的数据项？').then(function() {
        return delUserCourseInfos(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('course/userCourseInfos/export', {
        ...this.queryParams
      }, `userCourseInfos_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
